package com.se2.antiqueexhibition.Controller;


import com.se2.antiqueexhibition.Service.AdminActivityService;
import com.se2.antiqueexhibition.Service.AdminAnnouncementService;
import com.se2.antiqueexhibition.pojo.AdminActivity;
import com.se2.antiqueexhibition.pojo.AdminAnnouncement;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Controller
@RequestMapping("/Announcement")
public class AdminAnnouncementController {


    @Resource
    private AdminAnnouncementService adminAnnouncementService;

    @GetMapping("/list")
    public String listAdminAnnouncement(Model model){
        List<AdminAnnouncement> adminAnnouncement = adminAnnouncementService.getAdminAnnouncements();
        model.addAttribute("adminActivity", adminAnnouncement);
        return "adminAnnouncement-management";

    }



    @PostMapping("/insert")
    public String insert(@ModelAttribute AdminAnnouncement adminAnnouncement, Model model) {
        try{
            adminAnnouncementService.insert(adminAnnouncement);
            model.addAttribute("message","公告添加成功");
            model.addAttribute("success", true);
            return "redirect:list";
        }catch(Exception e){
            model.addAttribute("message","公告添加失败");
            model.addAttribute("success", false);
            model.addAttribute("adminAnnouncement", adminAnnouncement);
            return "adminAnnouncement-from";
        }
    }

    @PostMapping("/update")
    public String update(@ModelAttribute AdminAnnouncement adminAnnouncement, Model model) {
        try{
            adminAnnouncementService.update(adminAnnouncement);
            model.addAttribute("message","公告更新成功");
            model.addAttribute("success", true);
            return "redirect:list";
        }catch(Exception e){
            model.addAttribute("message","公告更新失败");
            model.addAttribute("success", false);
            model.addAttribute("adminAnnouncement", adminAnnouncement);
            return "adminAnnouncement-from";
        }
    }

    @GetMapping("/delete")
    public String delete(@RequestParam int id, Model model) {
        try{
            adminAnnouncementService.delete(id);
            model.addAttribute("message","公告删除成功");
            model.addAttribute("success", true);
        } catch (Exception e) {
            model.addAttribute("message","公告删除失败");
            model.addAttribute("success", false);

        }
        return "aminAnnouncement:list";

    }
}
